$(function () {
    loadIngStudyLine();



    $("body").on("click","#upWeekData",function(){
       var val = $('#week-picker').val();

        var week;
        if(val=="上一周"){
            week =getBeforeWeekDay( new Date());
        }else{
             week = getWeekDay(new Date());
            week ={endTime:week[6],
                startTime:week[0]
            }
        }
        $("#endTime").text(dateFormat("mm月dd日", week.endTime));
        $("#startTime").text(dateFormat("YYYY年mm月dd日", week.startTime));
        var  parameter={type:1,endTime:week.endTime,startTime:week.startTime};
        learningTrend(parameter,dealWithWeek);
        pickerChoose("#week-picker");
        learningNumerical(parameter);
    }).on("click","#upMonthesData",function(){
       var month =  $('#monthes-picker').val();
       var firstDate = getDate(month+"-01");
        firstDate.setDate(1); //第一天
     var week =   getFrsitMonthandLast(firstDate);
        $("#endTime").text(dateFormat("mm月dd日",getDate(week.endTime)));
        $("#startTime").text(dateFormat("YYYY年mm月dd日",getDate(week.startTime)));
        var  parameter={type:1,endTime:getDate(week.endTime),startTime:getDate(week.startTime)};
        learningTrend(parameter,dealWithMonth);
        pickerChoose("#monthes-picker");
        learningNumerical(parameter);
    }).on("click","#yearsData",function(){
       var yer =  $('#years-picker').val();
        var  parameter={type:2,endTime:yer+"-12-31T16:00:00.000Z",startTime:yer+"-01-01T16:00:00.000Z"};
        $("#endTime").text("12月31日");
        $("#startTime").text(yer+"年01月01日");
        learningTrend(parameter,dealWithYer);
        pickerChoose("#years-picker");
        learningNumerical(parameter);

    });

var m =new Array();
var y =new Array();
    //处理月份
    chuli();
    function chuli(){

        var diffDate = getDiffDate( new Date(dateData) ,new Date());
        $.each(diffDate,function(index,em){
            var mm = em.substring(0,7);
            var yy = em.substring(0,4);
            if( !m.includes(mm) ) {//includes 检测数组是否有某个值
                m.push(mm);
            }
            if( !y.includes(yy) ) {//includes 检测数组是否有某个值
                y.push(yy);
            }
        });

      m =  m.reverse()
      y =  y.reverse()

    }


    $('#week-picker').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
                                      <button class="button button-link pull-right close-picker" id="upWeekData">确定</button>\
                                      <h1 class="title">请选择</h1>\
                                      </header>',
        cols: [
            {
                textAlign: 'center',
                values: ['上一周','本周'],
                cssClass: "col-province"
            }
        ],
        value: ["上一周"]
    });
    //选择月
    $('#monthes-picker').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
                                      <button class="button button-link pull-right close-picker" id="upMonthesData">确定</button>\
                                      <h1 class="title">请选择月</h1>\
                                      </header>',
        cols: [
            {
                textAlign: 'center',
                values: m,
                cssClass: "col-province"
            }
        ],
        value: [m[0]]

    });
//选择年
    $('#years-picker').picker({
        toolbarTemplate: '<header class="bar bar-nav">\
                                      <button class="button button-link pull-right close-picker" id="yearsData">确定</button>\
                                      <h1 class="title">请选择年</h1>\
                                      </header>',
        cols: [
            {
                textAlign: 'center',
                values: y,
                cssClass: "col-province"
            }
        ],
        value: [y[0]]
    })
});


function loadIngStudyLine (){
    var newData = new Date();
    var week = getWeekDay(newData);
    $("#endTime").text(dateFormat("mm月dd日", week[6]));
    $("#startTime").text(dateFormat("YYYY年mm月dd日", week[0]));
    var  parameter={type:1,endTime:week[6],startTime:week[0]};
    learningTrend(parameter,dealWithWeek);
    learningNumerical(parameter);


}

function dealWithWeek(res){
    var labels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
    var duration = 0;
    var learTime = new Array();
    for(var i=0;i<res.length;i++){
        duration+=res[i].learntime;
        learTime.push(res[i].learntime);
    }
    studyLineFn (learTime,labels)

    $("#duration").text(duration);
}

function dealWithMonth(res){
    var labels =  new Array();
    var duration = 0;
    var learTime = new Array();
    for(var i=0;i<res.length;i++){
        duration+=res[i].learntime;
        learTime.push(res[i].learntime);
        labels.push(res[i].time.substring(res[i].time.length-2)+"日")
    }
    studyLineFn (learTime,labels)
    $("#duration").text(duration);
}

function dealWithYer(res){
    var labels =  new Array();
    var duration = 0;
    var learTime = new Array();
    for(var i=0;i<res.length;i++){
        duration+=res[i].learntime;
        learTime.push(res[i].learntime);
        labels.push(res[i].time.substring(res[i].time.length-2)+"月")
    }
    studyLineFn (learTime,labels)
    $("#duration").text(duration);
}


function studyLineFn(data,labels) {
    var studyConfig = {
        type: 'line',
        data: {
            labels: labels ,
            datasets: [{
                label: '学习时长(分钟)',
                backgroundColor: 'rgba(255, 178, 28,.5)',
                borderColor: 'rgb(255, 178, 28)',
                borderWidth: 1,
                data: data,
                fill: true,
            }]
        },
        options: {
            legend: {
                position: 'bottom'
            },
            responsive: true,
            title: {
                display: false,
            },
            tooltips: {
                mode: 'index',
                intersect: false,
            },
            hover: {
                mode: 'nearest',
                intersect: true
            },
            scales: {
                xAxes: [{
                    display: true,
                    gridLines: {
                        'lineWidth': 0
                    },
                    scaleLabel: {
                        display: false
                    }

                }],
                yAxes: [{
                    display: true,
                    gridLines: {
                        color: "rgba(0, 0, 0, 0.03)"
                    },


                    scaleLabel: {
                        display: false
                    },
                    ticks: {
                        min: 0,  //最小值
                    }

                }]
            }
        }
    };

    var ctx = document.getElementById('studyLineCanvas').getContext('2d');
    window.myLine = new Chart(ctx, studyConfig);

}


function pickerChoose(data){
    var value = $(data).val();
    $(".data-picker").parent("a").removeClass("active");
    $(data).parent("a").addClass("active");
    $('#years-picker').val("年");
    $('#monthes-picker').val("月");
    $('#week-picker').val("周");
    $(data).val(value);

}



/**
 * 根据日期查询学习数据
 * @param parameter
 */
function learningNumerical(parameter){

    parameter = JSON.stringify(parameter);

    $.ajax({
        url:baseloaction +'/mobile/uc/learningNumerical',
        type:'post',
        dataType:'json',
        contentType:'application/json;charset=utf-8',
        data: parameter,
        success:function (result) {

            $("#learnDayNum").text(result.learnDayNum);
            $("#signIn").text(result.signIn);
            $("#examRecordCount").text(result.examRecordCount);
        }

    })

}